﻿using SharpSoft.Data.DAOS;
using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.Web
{
    public class AppUser
    {
        public AppUser()
        {
        }
        public AppUser(AccountsDAO.User user)
        {
            this.UserId = user.Id;
            this.Ticket = user.Ticket;
            this.Status = user.Status;
            this.RoleFlag = user.RoleFlag;
            this.UserName = user.Name;
            this.DisplayName = user.DisplayName??this.UserName;
        }
        /// <summary>
        /// 是否为系统根账号（超级用户）
        /// </summary>
        public bool IsRootUser { get; internal set; } = false;

        public int UserId { get; internal set; }
        public string Ticket { get; internal set; }
        public int Status { get; internal set; }
        public string UserName { get; internal set; }
        public string DisplayName { get; internal set; }
        /// <summary>
        /// 用户角色，角色使用位标志定义 如：1:Admin 2:仓管员  4：采购员 8：销售员
        /// 那么当用户角色为10（bin:1010）时用户包含“销售员”|“仓管员”两种角色
        /// </summary>
        public int RoleFlag { get; internal set; }

        public int[] Roles { get; set; }
        /// <summary>
        /// 获取当前用户是否包含指定的角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public bool HasRole(int role)
        {
            return ((this.RoleFlag & role) == role);
        }
        public string Permissions { get; internal set; }
        /// <summary>
        /// 获取此用户是否具有指定的权限
        /// </summary>
        /// <param name="permissionName"></param>
        /// <returns></returns>
        public bool GetPermissionEnabled(string permissionName)
        {
            return AuthorizeProvider.PermissionMap.GetPermissionEnabled(this.Permissions, permissionName);
        }
    }
}
